package com.test.platform.ui.screen

import ProjectRepository
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.test.platform.model.Project
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.lifecycle.viewmodel.compose.viewModel
import com.test.platform.viewmodel.ProjectViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ProjectListScreen(
    modifier: Modifier = Modifier,
    viewModel: ProjectViewModel = viewModel {
        ProjectViewModel(ProjectRepository())
    }
) {
    var searchText by remember { mutableStateOf("") }
    
    Scaffold(
        modifier = modifier,
        topBar = {
            Column {
                TopAppBar(
                    title = { Text("项目列表") },
                    actions = {
                        IconButton(onClick = { /* TODO: 添加项目 */ }) {
                            Icon(Icons.Default.Add, "添加项目")
                        }
                    }
                )
                // 添加搜索栏
                OutlinedTextField(
                    value = searchText,
                    onValueChange = { searchText = it },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp),
                    placeholder = { Text("搜索项目") },
                    leadingIcon = { Icon(Icons.Default.Search, "搜索") },
                    singleLine = true
                )
            }
        },
        floatingActionButton = {
            FloatingActionButton(
                onClick = { /* TODO: 创建新项目 */ }
            ) {
                Icon(Icons.Default.Add, "创建项目")
            }
        }
    ) { padding ->
        val projects by viewModel.projects.collectAsState()
        val loading by viewModel.loading.collectAsState()
        
        LaunchedEffect(Unit) {
            viewModel.loadProjects()
        }
        
        if (loading) {
            CircularProgressIndicator(
                modifier = Modifier
                    .fillMaxSize()
                    .wrapContentSize()
            )
        } else {
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(padding)
            ) {
                items(projects) { project ->
                    ProjectCard(project)
                }
            }
        }
    }
}

@Composable
private fun ProjectCard(project: Project) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(8.dp)
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                text = "ID: ${project.id}",
                style = MaterialTheme.typography.titleMedium
            )
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                text = "组织: ${project.owner} 库: ${project.name}",
                style = MaterialTheme.typography.bodyMedium
            )
            Text(
                text = "描述: ${project.description}",
                style = MaterialTheme.typography.bodyMedium
            )
            Text(
                text = "Git: ${project.gitUrl}",
                style = MaterialTheme.typography.bodyMedium
            )
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.End
            ) {
                Button(
                    onClick = { /* TODO: 构建 */ },
                    modifier = Modifier.padding(end = 8.dp)
                ) {
                    Text("构建")
                }
                Button(
                    onClick = { /* TODO: 删除 */ },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.error
                    )
                ) {
                    Text("删除")
                }
            }
        }
    }
}